package Client;

import SQL.Connections;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

//旅客主页
public class PassengerTicketpage extends JFrame{
    private JPanel panel1;
    private JLabel greeting;//打招呼
    private JButton 退票Button;
    private JButton 返回Button;
    private JTable table1;
    public static JFrame f;

    Vector<Vector> values;
    Vector<String> name;
    public static String classno;//选择的班次号
    public static String user=new String(PassengerHome.user1);
    private Statement stm;
    private static String no1=new String(TicketRecord.a);//id身份证

    public PassengerTicketpage() {
        返回Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //跳转
                TicketRecord ticketRecord=new TicketRecord();
                ticketRecord.initialize(user);
                f.dispose();
            }
        });

        table1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                int rowINdex=table1.getSelectedRow();
                classno= (String) values.get(rowINdex).get(1);//表格选择行的班次号
            }
        });
        退票Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                int n=JOptionPane.showConfirmDialog(null,"是否退票","退票",JOptionPane.YES_NO_OPTION);
                if(n==0){//0为是
                    try {
                        String sql=("delete from ticket where TClassNumber='"+classno+"'");//退票
                        Connections connections = new Connections();
                        Connection con = connections.getConnection();
                        stm = con.createStatement();
                        int re = stm.executeUpdate(sql);

                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(null,"退票成功",null,JOptionPane.PLAIN_MESSAGE);
                }else{

                }
            }
        });
    }

    public void check(){

    }

    public static void initialize() {
        initialize(classno);

    }

    public static void main(String[] args) {
        initialize();


    }

    //初始化
    public static void initialize(String no) {
        JFrame frame = new JFrame("PassengerHomepage");
        frame.setContentPane(new PassengerTicketpage().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        f=frame;

    }


    private void createUIComponents() {
        // TODO: place custom component creation code here
        String sql=("SELECT * FROM Ticket where TIdCardNumber = '"+no1+"'");
        values=new Vector<>();
        name=new Vector();

        values= Connections.queryData(sql);
        name.add("车票号");
        name.add("班次号");
        name.add("座位号");
        name.add("购票人身份证号");
        name.add("价格");

        TableModel d=new DefaultTableModel(values,name);
        table1= new JTable(values,name);
        //table1.setRowSelectionInterval(0,0);
    }
}
